﻿using UnityEngine;
using UnityEngine.AI;

namespace DL.AI.AIBT.Example
{
    public class MoveTotarget : IStrategy
    {
        private readonly Transform m_Entity;
        private readonly NavMeshAgent m_Agent;
        private readonly Transform m_Target;
        bool m_IsPathCalculated;
        public MoveTotarget(Transform entity, NavMeshAgent agent, Transform target)
        {
            m_Entity = entity;
            m_Agent = agent;
            m_Target = target;
        }

        public Status Process()
        {
            if (Vector3.Distance(m_Entity.position, m_Target.position) < 1f) {
                return Status.Success;
            }
            
            m_Agent.SetDestination(m_Target.position);
            m_Entity.LookAt(m_Target);
            
            if (m_Agent.pathPending) {
                m_IsPathCalculated = true;
            }
            
            return Status.Running;
        }

        public void Reset()
        {
            m_IsPathCalculated = false;
        }
    }
}